/* 
 * oms，一个基于J2EE架构运营支撑平台
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.publish.impl;

import java.util.List;

import org.grayrabbit.cms.dao.publish.PublishLogDao;
import org.grayrabbit.cms.entity.PublishLog;
import org.grayrabbit.cms.vo.publish.PublishLogBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>PublishLog</b> dao implementation 

 * @version 1.0，创建时间：2012-01-05 17:51:25 
 */
@Repository
public class PublishLogDaoImpl extends GenericHibernateDao<PublishLog, String>
		implements PublishLogDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From PublishLog as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see PublishLogDao#assembly(PublishLogBean)
	 */
	public QueryCondition assembly(PublishLogBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see PublishLogDao#assemblyCount(PublishLogBean)
	 */
	public QueryCondition assemblyCount(PublishLogBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * @see PublishLogDao#count(PublishLogBean)
	 */
	public int count(PublishLogBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see PublishLogDao#assemblyQuery(PublishLogBean)
	 */
	public QueryCondition assermblyQuery(PublishLogBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		trans(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see PublishLogDao#assemblyQueryCount(PublishLogBean)
	 */
	public QueryCondition assemblyQueryCount(PublishLogBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		trans(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see PublishLogDao#findByPage(Page, PublishLogBean)
	 */
	public List<PublishLog> findByPage(Page<PublishLog> page,
			PublishLogBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void trans(PublishLogBean bean,QueryCondition qc){
		//TODO:兄弟们此处根据业务修改代码，完成后，请删除本行，沈永生于2012-01-05 17:51:25提醒!
		if(bean!=null){
		            if(bean.getPublishTime() != null){
				qc.add(" And obj.publishTime like ? ", "%"+bean.getPublishTime()+"%");
			}
            if(bean.getPublishStatus() != null){
				qc.add(" And obj.publishStatus like ? ", "%"+bean.getPublishStatus()+"%");
			}
            if(bean.getPublishInfo() != null){
				qc.add(" And obj.publishInfo like ? ", "%"+bean.getPublishInfo()+"%");
			}
            if(bean.getPublishType() != null){
				qc.add(" And obj.publishType like ? ", "%"+bean.getPublishType()+"%");
			}
            if(bean.getObjectId() != null){
				qc.add(" And obj.objectId like ? ", "%"+bean.getObjectId()+"%");
			}
            if(bean.getTemplatePath() != null){
				qc.add(" And obj.templatePath like ? ", "%"+bean.getTemplatePath()+"%");
			}
            if(bean.getHtmlPath() != null){
				qc.add(" And obj.htmlPath like ? ", "%"+bean.getHtmlPath()+"%");
			}
            if(bean.getIsError() != null){
				qc.add(" And obj.isError like ? ", "%"+bean.getIsError()+"%");
			}
            if(bean.getPublishTask() != null){
				qc.add(" And obj.cmsPublishTask like ? ", "%"+bean.getPublishTask()+"%");
			}

		}
	}
	
	public List<PublishLog> findByTaskId(String taskId, int offset) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.publishTask.id=? ", taskId);
		qc.appenOrderBy(" obj.publishTime asc ");
		qc.appenOrderBy(" obj.id asc ");
		qc.setOffset(offset);
		qc.setMaxResults(50);//一次性取50条记录
		return this.findByQc(qc);
	}

}